package com.kpx.app.activity;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.LocationDetails;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.bean.RegionNew;
import com.kpx.app.bean.User;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.listener.OnTimePickerListener;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.StringUtil;
import com.kpx.app.utils.TimeUtil;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.bigkoo.pickerview.OptionsPickerView;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by congdy on 17/3/4.
 * 修改用户信息
 */

public class ActivityUpdateUserinfo extends ActivityBase {
    private static LocationDetails loacl;
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.et_1)
    EditText et1;
    @BindView(R.id.txt_1)
    TextView txt1;
    @BindView(R.id.ll_1)
    LinearLayout ll1;
    @BindView(R.id.txt_2)
    TextView txt2;
    @BindView(R.id.txt_2_r)
    TextView txt2R;
    @BindView(R.id.txt_content)
    TextView txt_content;
    @BindView(R.id.ll_2)
    LinearLayout ll2;
    public static int updateType;
    private ArrayList<RegionNew> regionNews;
    private OptionsPickerView regionPickerView;
    private RegionNew regionNew;
    private RegionNew regionNew2;
    private RegionNew regionNew3;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_update_userinfo);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "");
        titleview.setRight("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkedData()) {
                    showLoadingDialog();
                    MyHashMap map = new MyHashMap();
                    setData(map);
                    UserInfoUtils.toUpdateData(ActivityUpdateUserinfo.this, map, new UserInfoUtils.OnUpdateUserInfoBackListener() {
                        @Override
                        public void back(boolean issuccess, String msg) {
                            dissmissLoadingDialog();
                            if (issuccess) {
                                if (updateType == ActivityUserInfo.TYPE_LOCATION) {
                                    LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
                                    if (loginUser != null) {
                                        User user = loginUser.getUSER();
                                        if (user != null) {
                                            user.setRegion(txt_content.getText().toString().trim());
                                            if (loacl != null) {
                                                user.setLat(loacl.getLatitude() + "");
                                                user.setLon(loacl.getLongitude() + "");
                                            }
                                            if (regionNew != null) {
                                                user.setProvinceId(regionNew.getDmId() + "");
                                            }
                                            if (regionNew2 != null) {
                                                user.setCityId(regionNew2.getDmId() + "");
                                            }
                                            if (regionNew3 != null) {
                                                user.setDistrictId(regionNew3.getDmId() + "");
                                            }
                                        }
                                    }
                                    UserInfoUtils.saveLoginUser(loginUser);
                                    finish();
                                    ToastUtil.getInstance().showToast(ActivityUpdateUserinfo.this, "保存成功");
                                } else if (updateType == ActivityUserInfo.TYPE_AGE) {
                                    UserInfoUtils.getNetLoginUser(ActivityUpdateUserinfo.this, new UserInfoUtils.OnGetNetInfoBack() {
                                        @Override
                                        public void back(LoginUser info) {
                                            finish();
                                            ToastUtil.getInstance().showToast(ActivityUpdateUserinfo.this, "保存成功");
                                        }
                                    });
                                } else {
                                    UserInfoUtils.updateUserInfoCache(updateType, et1.getText().toString().trim());
                                    finish();
                                    ToastUtil.getInstance().showToast(ActivityUpdateUserinfo.this, "保存成功");
                                }

                            }
                        }
                    });
                }
            }
        }, R.color.white);
        UserInfoUtils.getLocation(ActivityUpdateUserinfo.this, new UserInfoUtils.OnGetLocationBackListener() {
            @Override
            public void onLocationDataBack(LocationDetails locationDetails) {
                if (locationDetails != null) {
                    ActivityUpdateUserinfo.loacl = locationDetails;
                }
            }
        });
    }

    private boolean checkedData() {
        if (updateType == ActivityUserInfo.TYPE_LOCATION) {
            if (TextUtils.isEmpty(txt_content.getText().toString().trim())) {
                ToastUtil.getInstance().showToast(ActivityUpdateUserinfo.this, "请选择地区");
                return false;
            }
        } else if (updateType == ActivityUserInfo.TYPE_AGE) {
            if (TextUtils.isEmpty(txt_content.getText().toString().trim())) {
                ToastUtil.getInstance().showToast(ActivityUpdateUserinfo.this, "请选择年齡");
                return false;
            }
        } else {
            if (TextUtils.isEmpty(et1.getText().toString().trim())) {
                String mention = null;
                switch (updateType) {
                    case ActivityUserInfo.TYPE_NICK://昵称
                        mention = "请输入昵称";
                        break;
                    case ActivityUserInfo.TYPE_HEIGHT://身高
                        mention = "请输入身高";
                        break;
                    case ActivityUserInfo.TYPE_WEIGHT://体重
                        mention = "请输入体重";
                        break;
                    case ActivityUserInfo.TYPE_SHOSE_SIZE://鞋码
                        mention = "请输入鞋码";
                        break;
                    case ActivityUserInfo.TYPE_LOCATION://地区
                        mention = "请选择地区";
                        break;
                }
                ToastUtil.getInstance().showToast(ActivityUpdateUserinfo.this, mention);
                return false;
            }
        }
        return true;
    }

    /**
     * * 参数：token,headImg,nickName,sex(1男,2女)
     * ,region(广东省-深圳市),
     * birthDay(1990-05-20),
     * age(26),height(170),
     * weight(70),
     * shoeSize(鞋码42),
     * clothingSize(衣码由基础数据来),
     * bust(胸围),
     * waist(腰围),
     * cupSize(罩杯)
     *
     * @param map
     */
    private void setData(MyHashMap map) {
        switch (updateType) {
            case ActivityUserInfo.TYPE_NICK://昵称
                map.put("nickName", et1.getText().toString().trim());
                break;
            case ActivityUserInfo.TYPE_HEIGHT://身高
                map.put("height", et1.getText().toString().trim());
                break;
            case ActivityUserInfo.TYPE_WEIGHT://体重
                map.put("weight", et1.getText().toString().trim());
                break;
            case ActivityUserInfo.TYPE_SHOSE_SIZE://鞋码
                map.put("shoeSize", et1.getText().toString().trim());
                break;
            case ActivityUserInfo.TYPE_LOCATION://地区lon(经度),lat(纬度),provinceId(省ID),cityId(市ID),districtId(区ID)
                if (loacl != null) {
                    map.put("lon", loacl.getLongitude() + "");
                    map.put("lat", loacl.getLatitude() + "");
                }
                map.put("region", txt_content.getText().toString().trim());
                map.put("provinceId", regionNew.getDmId() + "");
                map.put("cityId", regionNew2.getDmId() + "");
                break;
            case ActivityUserInfo.TYPE_AGE://年龄
                map.put("birthDay", txt_content.getText().toString().trim());
                break;
        }
    }

    @Override
    public void initData() {
        ll1.setVisibility(View.GONE);
        ll2.setVisibility(View.GONE);
        txt1.setBackgroundColor(getResources().getColor(R.color.white));
        User user = UserInfoUtils.getCachedLoginUser().getUSER();
        switch (updateType) {
            case ActivityUserInfo.TYPE_NICK://昵称
                titleview.setTitle("昵称");
                Drawable drawable = getResources().getDrawable(R.drawable.icon_delete);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                txt1.setCompoundDrawables(drawable, null, null, null);
                ll1.setVisibility(View.VISIBLE);
                et1.setInputType(InputType.TYPE_CLASS_TEXT);
                txt1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        et1.setText("");
                    }
                });
                if (user != null && !UIUtils.isEmpty(user.getNickName())) {
                    et1.setHint(user.getNickName());
                }
                break;
            case ActivityUserInfo.TYPE_CLOTH_SIZE://衣服尺码
                titleview.setTitle("衣服尺码");
                drawable = getResources().getDrawable(R.drawable.arrow_down);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                txt1.setCompoundDrawables(drawable, null, null, null);
                et1.setInputType(InputType.TYPE_CLASS_NUMBER);
                ll1.setVisibility(View.VISIBLE);
                if (user != null && !UIUtils.isEmpty(user.getClothingSize())) {
                    et1.setHint(user.getClothingSize());
                }
                break;
            case ActivityUserInfo.TYPE_HEIGHT://身高
                titleview.setTitle("身高");
                txt1.setText("cm");
                ll1.setVisibility(View.VISIBLE);
                et1.setInputType(InputType.TYPE_CLASS_NUMBER);
                if (user != null && !UIUtils.isEmpty(user.getHeight())) {
                    et1.setHint(user.getHeight());
                }
                break;
            case ActivityUserInfo.TYPE_WEIGHT://体重
                titleview.setTitle("体重");
                txt1.setText("kg");
                ll1.setVisibility(View.VISIBLE);
                et1.setInputType(InputType.TYPE_CLASS_NUMBER);
                if (user != null && !UIUtils.isEmpty(user.getWeight())) {
                    et1.setHint(user.getWeight());
                }
                break;
            case ActivityUserInfo.TYPE_SHOSE_SIZE://鞋码
                titleview.setTitle("鞋码");
                txt1.setText("码");
                ll1.setVisibility(View.VISIBLE);
                et1.setInputType(InputType.TYPE_CLASS_NUMBER);
                if (user != null && !UIUtils.isEmpty(user.getShoeSize())) {
                    et1.setHint(user.getShoeSize());
                }
                break;
            case ActivityUserInfo.TYPE_LOCATION://地区
                titleview.setTitle("地区");
                txt2.setText("地区");
                ll2.setVisibility(View.VISIBLE);
                if (user != null && !UIUtils.isEmpty(user.getRegion())) {
                    txt_content.setHint(user.getRegion());
                }
                break;
            case ActivityUserInfo.TYPE_AGE://年龄
                titleview.setTitle("年龄");
                txt2.setText("年龄");
                ll2.setVisibility(View.VISIBLE);
                findViewById(R.id.txt_2_r).setVisibility(View.INVISIBLE);
                if (user != null && !UIUtils.isEmpty(user.getBirthDay())) {
                    txt_content.setHint(user.getBirthDay());
                }
                break;
        }
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    final Calendar c = Calendar.getInstance();

    @OnClick({R.id.txt_1, R.id.txt_2_r, R.id.txt_content})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_content:
                if (updateType == ActivityUserInfo.TYPE_LOCATION) {
                    showSelectLocation();
                } else if (updateType == ActivityUserInfo.TYPE_AGE) {
                    TimeUtil.showBirthdayPickDialog(ActivityUpdateUserinfo.this, c, "选择生日", new OnTimePickerListener() {
                        @Override
                        public void onTimePicked(long time) {
                            txt_content.setText(DateFormat.format("yyy-MM-dd", time));
                        }
                    });
                }
                break;
            case R.id.txt_1:
                break;
            case R.id.txt_2_r:
                requestToLocation(new OnGetLocationPermissionBack() {
                    @Override
                    public void back() {
                        if (updateType == ActivityUserInfo.TYPE_LOCATION) {
                            showSelectLocation();
                        }
//                toLocation();
                    }
                });
                break;
        }
    }

    /**
     * 展示选择地址的窗口
     */
    private void showSelectLocation() {
        showLoadingDialog();
        UserInfoUtils.getPCdata(ActivityUpdateUserinfo.this, new UserInfoUtils.OnGetPCdataBackListener() {
            @Override
            public void getData(ArrayList<RegionNew> regions) {
                dissmissLoadingDialog();
                regionNews = regions;
                if (regionNews != null) {
                    showProviceList();
                }
            }
        });
    }

    // 显示省市选择对话框
    public void showProviceList() {
        if (null == regionPickerView) {
            regionPickerView = new OptionsPickerView(ActivityUpdateUserinfo.this);
            ArrayList<String> options1 = new ArrayList<>();
            ArrayList<ArrayList<String>> options2 = new ArrayList<>();
//            ArrayList<ArrayList<ArrayList<String>>> options3 = new ArrayList<>();
            ArrayList<String> data2;
//            ArrayList<String> data3;
//            ArrayList<ArrayList<String>> data4;
            if (null != regionNews && regionNews.size() > 0) {
                for (RegionNew r : regionNews) {
                    options1.add(r.getName() + "");
                    data2 = new ArrayList<>();
//                    data4 = new ArrayList<>();
                    ArrayList<RegionNew> citys = r.getRegions();
                    if (citys != null) {
                        for (RegionNew city : citys) {
                            data2.add(city.getName() + "");
//                            data3 = new ArrayList<>();
//                            ArrayList<RegionNew> qus = city.getRegions();
//                            if (qus != null) {
//                                for (RegionNew q : qus) {
//                                    data3.add(q.getName() + "");
//                                }
//                            }
//                            data4.add(data3);
                        }
                    }
                    options2.add(data2);
//                    options3.add(data4);
                }
            }
            regionPickerView.setPicker(options1, options2, true);
//            regionPickerView.setPicker(options1, options2, options3, true);
            regionPickerView.setCyclic(false);
        }
        regionPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                regionNew = regionNews.get(options1);
                regionNew2 = regionNew.getRegions().get(option2);
//                regionNew3 = regionNew.getRegions().get(options3);
                txt_content.setText(regionNew.getName() + regionNew2.getName());
            }
        });
        if (!regionPickerView.isShowing()) {
            regionPickerView.show();
        }
    }

    /**
     * 进行定位操作
     */
    private void toLocation() {
        if (loacl != null) {
            String titleFromLocation = UserInfoUtils.getPCFromLocation(loacl);
            if (!StringUtil.isEmpty(titleFromLocation)) {
                txt_content.setText(titleFromLocation);
            }
        } else {
            UserInfoUtils.getLocation(ActivityUpdateUserinfo.this, new UserInfoUtils.OnGetLocationBackListener() {
                @Override
                public void onLocationDataBack(LocationDetails locationDetails) {
                    if (locationDetails != null) {
                        ActivityUpdateUserinfo.loacl = locationDetails;
                        String titleFromLocation = UserInfoUtils.getPCFromLocation(locationDetails);
                        if (!StringUtil.isEmpty(titleFromLocation)) {
                            txt_content.setText(titleFromLocation);
                        }
                    }
                }
            });
        }
    }
}
